package com.lhy.admin.web;

import com.lhy.admin.Constants;
import com.lhy.admin.common.secure.authentication.Authentication;
import com.lhy.admin.common.secure.authorization.Permissions;
import com.lhy.admin.common.treeable.Treeable;
import com.lhy.admin.common.treeable.TreeableUtil;
import com.lhy.admin.model.Dict;
import com.lhy.admin.model.extend.Menu;
import com.lhy.admin.model.Permission;
import com.lhy.admin.model.extend.PermissionGroup;
import com.lhy.admin.model.extend.PermissionTree;
import com.lhy.admin.model.extend.RolePermissions;
import com.lhy.admin.service.DictService;
import com.lhy.admin.service.PermissionService;
import com.lhy.admin.service.RolePermissionService;
import com.lhy.admin.util.AuthUtil;
import com.lhy.commonweb.model.ResponseResult;
import com.lhy.commonweb.service.AbstractService;
import com.lhy.commonweb.web.BaseControllerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("permission")
@Permissions
public class PermissionController extends BaseControllerImpl<Permission, Integer> {

    @Autowired
    AuthUtil authUtil;

    @Autowired
    DictService dictService;

    @Autowired
    RolePermissionService rolePermissionService;

    public PermissionController(@Qualifier("permissionService") AbstractService service) {
        super(service);
    }

    @RequestMapping(value = "listMenus", method = RequestMethod.GET)
    @Authentication
    @Deprecated
    public ResponseResult listMenus() {
        List<Permission> permissions = authUtil.getPermission();
        List<Menu> collect = permissions.stream()
                .map(permission -> new Menu(permission.getCode(), "/" + permission.getCode(), permission.getName(), permission.getLevel(), permission.getPriority()))
                .sorted(Comparator.comparing(Menu::getPriority))
                .collect(Collectors.toList());
        return ResponseResult.ok().setData(collect);
    }

    @RequestMapping(value = "listMenuTree", method = RequestMethod.GET)
    @Authentication
    public ResponseResult listMenuTree() {
        List<Permission> permissions = authUtil.getPermission().stream()
                //只查询页面权限
                .filter(permission -> permission.getType().equals(0))
                .sorted(Comparator.comparing(Permission::getPriority))
                .collect(Collectors.toList());
        return ResponseResult.ok().setData(TreeableUtil.toTree(permissions, PermissionTree.class));
    }

    @Permissions("permission.view")
    @RequestMapping(value = "tree", method = RequestMethod.GET)
    public List<Treeable> tree(@Validated @ModelAttribute Permission bean) {
        List<Permission> list = super.queryCond(bean);
        return TreeableUtil.toTree(list, PermissionTree.class);
    }

    @Permissions("permission.view")
    @RequestMapping(value = "defaultPermissionGroup", method = RequestMethod.GET)
    public List<Dict> defaultPermissionGroup() {
        return dictService.query(new Dict().setCode(Constants.DEFAULT_PERMISSION_GROUP).setStatus(1));
    }

    @Permissions("permission.add")
    @RequestMapping(value = "group", method = RequestMethod.POST)
    public ResponseResult createGroup(@RequestBody PermissionGroup bean) {
        PermissionService.class.cast(service).savePermissionWithGroup(bean);
        return ResponseResult.ok();
    }

    @Override
    @Permissions("permission.edit")
    public ResponseResult update(@RequestBody Permission bean) {
        if (bean.getParent() == null) {
            bean.setParent(0);
        }
        return ResponseResult.create(this.service.update(bean));
    }

    @Override
    @Permissions("permission.del")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return ResponseResult.create(PermissionService.class.cast(service).delPermission(id));
    }

    /**
     * @param name
     * @return java.util.List<com.lhy.admin.common.treeable.Treeable>
     * @throws
     * @Title: rolePermission
     * @Description: 查询角色的所有权限
     * @date 2018/6/12 17:30
     */
    @Permissions("permission.view")
    @RequestMapping(value = "rolePermission/{name}", method = RequestMethod.GET)
    public List<Permission> rolePermission(@PathVariable("name") String name) {
        return PermissionService.class.cast(service).listPermissionsByRoleName(name);
    }

    /**
     * 更新角色权限关系
     */
    @Permissions({"permission.edit", "permission.add", "permission.del"})
    @RequestMapping(value = "updateRolePermissions", method = RequestMethod.POST)
    public ResponseResult updateRolePermissions(@RequestBody RolePermissions rolePermissions) {
        rolePermissionService.updateRolePermission(rolePermissions);
        return ResponseResult.ok();
    }


}
